﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Security.Cryptography;
using System.Security.Policy;
using System.IO;


namespace ElectronicJournal.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
        }

        protected void SendToMail(string userName, string password, string email)
        {
            string From = "ElectronicJournalSite@gmail.com";
            SmtpClient client = new SmtpClient("smtp.gmail.com");
            client.EnableSsl = true;
            client.Credentials = new NetworkCredential("ElectronicJournalSite@gmail.com", ";ehyfk*()");

            string to = email;
            MailMessage message = new MailMessage(From, to);    //  

            message.Subject = "Сообщение Электронного Журанала. Успешная регистрация.";
            message.BodyEncoding = System.Text.Encoding.UTF8;
            message.IsBodyHtml = false;
            
            var currentUser = Membership.GetUser(userName);
            
            message.Body = "Вы только что зарегистрировались на сайте электронного журнала! Ваше имя пользователя: " 
                + RegisterUser.UserName + "\n Ваш пароль: " + RegisterUser.Password;
            message.Body += "\n Для активации Вашего аккаунта пройдите по ссылке: " + "http://"
                + Request.ServerVariables["HTTP_HOST"] + @"/" +
                "Account/ContinueRegistration.aspx?ticket=" + currentUser.ProviderUserKey.ToString();
            
            client.Send(message);
        }
        
        protected void RegisterUser_CreatedUser(object sender, EventArgs e)
        {
            SendToMail(RegisterUser.UserName, RegisterUser.Password, RegisterUser.Email);

            MembershipUser userCreated = Membership.GetUser(RegisterUser.UserName);
            Roles.AddUserToRole(RegisterUser.UserName, "unconfirmed");

            var key = userCreated.ProviderUserKey;

            FormsAuthentication.SetAuthCookie(RegisterUser.UserName, false /* createPersistentCookie */);
            string continueUrl = RegisterUser.ContinueDestinationPageUrl;
            if (String.IsNullOrEmpty(continueUrl))
            {
                continueUrl = "~/";
            }
            Response.Redirect(continueUrl);
        }

        public void RegisterUser_SendMailError(object sender, SendMailErrorEventArgs e)
        {
            //var ex = e.Exception;
        }


        public void RegisterUser_SendingMail(object sender, MailMessageEventArgs e)
        {
            //e.Message.Body = "Привет, Мир!";
            //e.Message.IsBodyHtml = false;
            //e.Message.Subject = "New user on Web site.";

            ////e.Message.Body = e.Message.Body.Replace("<%PasswordQuestion%>", Createuserwizard1.Question);
            ////e.Message.Body = e.Message.Body.Replace("<%PasswordAnswer%>",   Createuserwizard1.Answer);  
        }

    }
}
